# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web
import xml.dom.minidom
import time,os,json
import subprocess,threading
import xml.etree.ElementTree as ET
import asyncio
DATA1_G=''
DATA2_G=''
DATA3_G=''
class get_files1Handler(tornado.web.RequestHandler):
    def get(self):
        directory = './share_file/1/'  # 替换为实际的目录路径
        files = (os.listdir(directory))
        self.write(json.dumps(files))
class get_files2Handler(tornado.web.RequestHandler):
    def get(self):
        directory = './share_file/2/'  # 替换为实际的目录路径
        files = (os.listdir(directory))
        self.write(json.dumps(files))
class get_files3Handler(tornado.web.RequestHandler):
    def get(self):
        directory = './share_file/3/'  # 替换为实际的目录路径
        files = (os.listdir(directory))
        self.write(json.dumps(files))
class HomeHandler(tornado.web.RequestHandler):
    def get(self):  # 响应以get方式发送的请求
        dom = xml.dom.minidom.parse('cloud_data.txt')
        root = dom.documentElement
        test_data_dir = root.getElementsByTagName('test_data_dir')[0].childNodes[0].data.replace('"', '')
        test_data_dir_marked = root.getElementsByTagName('test_data_dir_marked')[0].childNodes[0].data.replace('"', '')
        output_data_dir = root.getElementsByTagName('output_data_dir')[0].childNodes[0].data.replace('"', '')
        rate = root.getElementsByTagName('rate')[0].childNodes[0].data.replace('"', '')
        IoU = root.getElementsByTagName('IoU')[0].childNodes[0].data.replace('"', '')
        Acc = root.getElementsByTagName('Acc')[0].childNodes[0].data.replace('"', '')

        self.render("index.html",
                    test_data_dir=test_data_dir,
                    test_data_dir_marked=test_data_dir_marked,
                    output_data_dir=output_data_dir,
                    rate=rate,
                    IoU=IoU,
                    Acc=Acc
                    )


class HomeHandler2(tornado.web.RequestHandler):
    def get(self):  # 响应以get方式发送的请求
        global DATA1_G,DATA2_G,DATA3_G
        #pathstr = self.get_query_argument('data', '')  # 如果没有参数时，返回空字符串
        path_arr=[]# = pathstr.split(",")
        path_arr.append(self.get_query_argument('data1', ''))
        path_arr.append(self.get_query_argument('data2', ''))
        path_arr.append(self.get_query_argument('data3', ''))
        DATA1_G=self.get_query_argument('data1', '')
        DATA2_G=self.get_query_argument('data2', '')
        DATA3_G=self.get_query_argument('data3', '')
        doc = xml.dom.minidom.Document()
        root = doc.createElement('root')


        name = doc.createElement('test_data_dir')
        name.appendChild(doc.createTextNode(path_arr[0]))
        root.appendChild(name)

        name = doc.createElement('test_data_dir_marked')
        name.appendChild(doc.createTextNode(path_arr[1]))
        root.appendChild(name)

        name = doc.createElement('output_data_dir')
        name.appendChild(doc.createTextNode(path_arr[2]))
        root.appendChild(name)

        name = doc.createElement('rate')
        name.appendChild(doc.createTextNode("0"))
        root.appendChild(name)

        name = doc.createElement('IoU')
        name.appendChild(doc.createTextNode("0%"))
        root.appendChild(name)

        name = doc.createElement('Acc')
        name.appendChild(doc.createTextNode("0%"))
        root.appendChild(name)
        doc.appendChild(root)
        print(doc)
        with open('cloud_data.txt', 'wb') as f:
            f.write(doc.toprettyxml(encoding='utf-8'))
            f.close()




def subp(test_data_dir,output_data_dir):
    execParam = 'python tool/test_las_direct.py --data_dir ' + test_data_dir + ' --result_dir ' + output_data_dir
    print('to exec python:' + execParam)
    os.system(execParam)


class HomeHandler3(tornado.web.RequestHandler):
    def get(self):  # 响应以get方式发送的请求
        global DATA1_G, DATA3_G
        assert DATA1_G!="" and DATA2_G!="" and DATA3_G!=""
        assert DATA1_G!="1" and DATA2_G!="2" and DATA3_G!="3"
        print("执行脚本")

        dom = xml.dom.minidom.parse('cloud_data.txt')
        root = dom.documentElement
        test_data_dir = root.getElementsByTagName('test_data_dir')[0].childNodes[0].data.replace('"', '')
        test_data_dir_marked = root.getElementsByTagName('test_data_dir_marked')[0].childNodes[0].data.replace('"', '')
        output_data_dir = root.getElementsByTagName('output_data_dir')[0].childNodes[0].data.replace('"', '')

        # test_dir = root.find('test_data_dir')
        # re_dir = root.find('output_data_dir')
        # print(test_dir.text)
        # subprocess.run(['python', 'tool/test_las_direct.py', '--data_dir', test_dir.text, '--result_dir', re_dir.text])
        print("output_data_dir: " + output_data_dir)
        test_dir = './share_file/1/'+DATA1_G+"/"
        re_dir = './share_file/3/'+ DATA3_G+"/"
        #subprocess.run(['python', 'tool/test_las_direct.py', '--data_dir', test_dir, '--result_dir', re_dir])
        thread = threading.Thread(target=subp, args=[test_dir,re_dir])
        thread.start()
        self.write("begin to exec")

class HomeHandler4(tornado.web.RequestHandler):
    def get(self):  # 响应以get方式发送的请求
        test_data_dir = self.get_query_argument('test_data_dir', '')  #
        test_data_dir_marked = self.get_query_argument('test_data_dir_marked', '')
        output_data_dir = self.get_query_argument('output_data_dir', '')  # 如果没有参数时，返回空字符串        # 如果没有参数时，返回空字符串
        rate = self.get_query_argument('rate', '')  # 如果没有参数时，返回空字符串
        IoU = self.get_query_argument('IoU', '')  # 如果没有参数时，返回空字符串
        Acc = self.get_query_argument('Acc', '')  # 如果没有参数时，返回空字符串
        self.render("index.html",
                    test_data_dir=test_data_dir,
                    test_data_dir_marked=test_data_dir_marked,
                    output_data_dir=output_data_dir,
                    rate=rate,
                    IoU=IoU,
                    Acc=Acc
                    )

class ProgressHadler(tornado.web.RequestHandler):
    def get(self):  # 响应以get方式发送的请求
        dom = xml.dom.minidom.parse('cloud_data.txt')
        root = dom.documentElement
        rate = root.getElementsByTagName('rate')[0].childNodes[0].data.replace('"', '')
        IoU = root.getElementsByTagName('IoU')[0].childNodes[0].data.replace('"', '')
        Acc = root.getElementsByTagName('Acc')[0].childNodes[0].data.replace('"', '')
        if int(rate)>100:
            rate="95"
        #print("rate:" + rate)
        self.write(rate + ";" + IoU + ";" + Acc)

        
app = tornado.web.Application([(r"/", HomeHandler),
                               (r"/setdata", HomeHandler2),
                               (r"/exePython", HomeHandler3),
                               (r"/updateData", HomeHandler4),
                               (r"/get_files1", get_files1Handler),
                               (r"/get_files2", get_files2Handler),
                               (r"/get_files3", get_files3Handler),
                                (r"/get_progress", ProgressHadler)
                               ])  # URL映射
app.listen(port=8877,address="0.0.0.0")  # 绑定侦听端口
tornado.ioloop.IOLoop.instance().start()  # 启动服务
